package Client;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.ImageIcon;

/**
 * random card
 * @author Termchai 5510546042, Eknarin 5510546239
 *
 */
public class Game implements Serializable{
	
	
	private final String[] name = new String[] {"aof","arm","aun","benz","dream","earth","ice","knot","kok","mai","mike","nop","oat","takor","term"};
	
	
	//private final Integer[] src = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
	private Card[][] card;
	private ArrayList<String> tempCard;
	private ArrayList<ImageIcon> icons;
	private ArrayList<int[]> location;
	
	/**
	 * random card when create Game
	 */
	public Game ()
	{
		randomCard();
	}
	/**
	 * random card
	 */
	public void randomCard()
	{
		/**
		tempCard = new int [][]{ {1,3,1,5,4,6} , {2,2,4,5,3,9} , {6,9,12,7,7,13} , {10,8,15,12,14,8} , {14,10,11,13,11,15}};
		
		card = new Card[5][6];
		for (int i=0; i<5; i++)
			for (int j=0; j<6; j++)
				card[i][j] = new Card(tempCard[i][j],new int[] {i,j});
		*/
		
		
		/**
		tempCard = new ArrayList<Integer>();
		for (Integer temp : src)
		{
			tempCard.add(temp);
		}
		Integer x,y,a,b;
		card = new Card[5][6];
		while (tempCard.size()!=0)
		{
			
			do
			{
				x = getRandomNumber(5);
				y = getRandomNumber(6);
				a = getRandomNumber(5);
				b = getRandomNumber(5);
			}while (card[x][y]!=null || card[a][b]!=null);
			
			card[x][y] = new Card(tempCard.get(0),new int[] {x,y});
			card[a][b] = new Card(tempCard.remove(0),new int[] {a,b});
		}
		
		for (Card[] temp : card)
		{
			for (Card tempp : temp)
			System.out.println(tempp+"");
		}
		*/
		Class clazz = this.getClass();
		location = new ArrayList<int[]>();
		tempCard = new ArrayList<String>();
		for (String temp : name)
		{
			tempCard.add(temp);
		}
		card = new Card[5][6];
		
		for (int i=0; i<5; i++)
		{
			for (int j=0; j<6; j++)
			{
				location.add(new int[]{i,j});
			}
		}
		icons = new ArrayList<ImageIcon>();
		while(tempCard.size()!=0)
		{
			URL m = clazz.getResource("/images/"+tempCard.remove(0)+".jpg");
			icons.add(new ImageIcon (m));
		}
		
		while (icons.size()!=0)
		{
			System.out.println(location.size()+"");
			System.out.println(icons.size()+"");
			
			int x =getRandomNumber(location.size());
			card[location.get(x)[0]][location.get(x)[1]] = new Card(icons.get(0),new int[] {location.get(x)[0],location.get(x)[1]});
			location.remove(x);
			
			x =getRandomNumber(location.size());
			card[location.get(x)[0]][location.get(x)[1]] = new Card(icons.remove(0),new int[] {location.get(x)[0],location.get(x)[1]});
			location.remove(x);
		}
		
		for (Card[] temp : card)
		{
			for (Card tempp : temp)
				System.out.println(tempp+"");
		}
	}

	/**
	 * 
	 * @param x index x
	 * @param y index y
	 * @return return card
	 */
	public Card getCard(int x,int y)
	{
		return card [x] [y];
	}
	
	/**
	 * 
	 * @return all Card
	 */
	public Card[][] getDeck()
	{
		return card;
	}
	/**
	 * random number use in randomCard
	 * @param limit limit of number index
	 * @return random number
	 */
    private int getRandomNumber(int limit) {
        // use a seed so the random numbers are not repeatable
        long seed = System.nanoTime( );
		Random random = new Random(seed);
//TODO There is a bug here.  Look at the Java API documentation
// for the nextInt() method of Random and fix the bug.
		return random.nextInt( limit );
    }
}
